// NodeSpace.h
// think more..
// by Mina Jung @ syracuse university
// April 2010


#import <random.h>
#import "EnvSpot.h"
#import <space.h>
#import <simtools.h>
#import <misc.h>
#import <math.h>




@implementation EnvSpot

- createEnd 
{
  [super createEnd];

  numData = 0;
  wall = 0;

  listOfData = [List create: [self getZone]];

  
  return self;
}

- (int)isWall
{
  return wall;
}

- setWall
{
  wall = 1;
  return self;
}


- setSensor
{
  wall = 2;
  return self;
}

- putRange:(int)t Min: (int)bot Max: (int)top
{
  id aData;

  if( wall > 0 )
    return self;

  aData = [Data create: [self getZone]];

  [aData putRange:t Min:bot Max:top];

  numData++;

  [listOfData addLast: aData];

  return self;
}

- setSpotValue:(int)val type: (int)t
{
  id index;
  id aData;
  int type;

  if(wall > 0)
    return self;

  index = [listOfData begin: [self getZone]];

  while( (aData = [index next]) )
  {
    type = [aData getDataType];

    if( type  == t )
    {
      [aData setDataValue: val];
      break;
    }

  }
  
  [index drop];

  return self;
}

- (int)getSpotValue:(int)t
{
  id index, aData;
  int val;

  if(wall == 1)
    return -1;
  else if(wall == 2)
    return 0;

  index = [listOfData begin: [self getZone]];
  while( (aData = [index next]) )
  {
    if( [aData getDataType] == t )
    {
      val = [aData getDataValue];
      break;
    }

  }

  [index drop];
  return (int)val;
}


- (int)getEnvValue
{
  id index, aData;
  int val = 0, t, v;               // 0 <-- normal, 1 <-- abnormal

  if(wall)
    return -1;

  index = [listOfData begin: [self getZone]];
  while( (aData = [index next]) )
  {
    v = [aData getDataValue];
    t = [aData getDataType];

    val += pow(3, t) * v;
  }

  [index drop];


  return (int)val%255;
}

- (int)getNumData
{
  return (int)numData;
}


@end



